﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using AutoR.Helpers;
using AutoR.Messages;
using AutoR.Models;
using AutoR.Services;
using LiteDB;
using Prism.Mvvm;
using Xamarin.Forms;

namespace AutoR.Tasks
{
	// Token: 0x0200010A RID: 266
	public class RepairTask : BindableBase
	{
		// Token: 0x17000186 RID: 390
		// (get) Token: 0x0600059E RID: 1438 RVA: 0x000408F6 File Offset: 0x0003EAF6
		protected GameData Game
		{
			get
			{
				return Runtime.Game;
			}
		}

		// Token: 0x17000187 RID: 391
		// (get) Token: 0x0600059F RID: 1439 RVA: 0x00057602 File Offset: 0x00055802
		// (set) Token: 0x060005A0 RID: 1440 RVA: 0x0005760C File Offset: 0x0005580C
		public int Id
		{
			[CompilerGenerated]
			get
			{
				return this.<Id>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Id>k__BackingField == value)
				{
					return;
				}
				this.<Id>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Id);
			}
		}

		// Token: 0x17000188 RID: 392
		// (get) Token: 0x060005A1 RID: 1441 RVA: 0x00057639 File Offset: 0x00055839
		// (set) Token: 0x060005A2 RID: 1442 RVA: 0x00057644 File Offset: 0x00055844
		public string Type
		{
			[CompilerGenerated]
			get
			{
				return this.<Type>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (string.Equals(this.<Type>k__BackingField, value, StringComparison.Ordinal))
				{
					return;
				}
				this.<Type>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Type);
			}
		}

		// Token: 0x17000189 RID: 393
		// (get) Token: 0x060005A3 RID: 1443 RVA: 0x00057675 File Offset: 0x00055875
		// (set) Token: 0x060005A4 RID: 1444 RVA: 0x00057680 File Offset: 0x00055880
		public bool Enabled
		{
			[CompilerGenerated]
			get
			{
				return this.<Enabled>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Enabled>k__BackingField == value)
				{
					return;
				}
				this.<Enabled>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Enabled);
			}
		}

		// Token: 0x1700018A RID: 394
		// (get) Token: 0x060005A5 RID: 1445 RVA: 0x000576AD File Offset: 0x000558AD
		// (set) Token: 0x060005A6 RID: 1446 RVA: 0x000576B8 File Offset: 0x000558B8
		[BsonIgnore]
		public Queue<UserShip> RepairQueue
		{
			[CompilerGenerated]
			get
			{
				return this.<RepairQueue>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<RepairQueue>k__BackingField, value))
				{
					return;
				}
				this.<RepairQueue>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.RepairQueue);
			}
		}

		// Token: 0x1700018B RID: 395
		// (get) Token: 0x060005A7 RID: 1447 RVA: 0x000576E8 File Offset: 0x000558E8
		// (set) Token: 0x060005A8 RID: 1448 RVA: 0x000576F0 File Offset: 0x000558F0
		public int MinHours
		{
			[CompilerGenerated]
			get
			{
				return this.<MinHours>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<MinHours>k__BackingField == value)
				{
					return;
				}
				this.<MinHours>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.MinHours);
			}
		}

		// Token: 0x1700018C RID: 396
		// (get) Token: 0x060005A9 RID: 1449 RVA: 0x0005771D File Offset: 0x0005591D
		// (set) Token: 0x060005AA RID: 1450 RVA: 0x00057728 File Offset: 0x00055928
		public int MinMinutes
		{
			[CompilerGenerated]
			get
			{
				return this.<MinMinutes>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<MinMinutes>k__BackingField == value)
				{
					return;
				}
				this.<MinMinutes>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.MinMinutes);
			}
		}

		// Token: 0x1700018D RID: 397
		// (get) Token: 0x060005AB RID: 1451 RVA: 0x00057755 File Offset: 0x00055955
		// (set) Token: 0x060005AC RID: 1452 RVA: 0x00057760 File Offset: 0x00055960
		public bool RepairOrder
		{
			[CompilerGenerated]
			get
			{
				return this.<RepairOrder>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<RepairOrder>k__BackingField == value)
				{
					return;
				}
				this.<RepairOrder>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.RepairOrder);
			}
		}

		// Token: 0x060005AD RID: 1453 RVA: 0x00057790 File Offset: 0x00055990
		public async Task Start()
		{
			foreach (RepairDockInfo dock in from i in this.Game.RepairDock
			where i.shipId != 0
			select i)
			{
				if (dock.rubdown == 0 && Runtime.Server.Version != "3.3.3")
				{
					Repair repair = await Runtime.Remote.Rubdown(dock.shipId);
					this.Game.RepairDock = new ObservableCollection<RepairDockInfo>(repair.repairDockVo.Where((RepairDockInfo i) => i.locked == 0));
					MessagingCenter.Send<LogMessage>(new LogMessage("[修理]奋力搓澡" + dock.Name), "Log");
					await this.Game.Delay(2000);
				}
				if (dock.endTime < DateTime.Now)
				{
					Repair repair2 = await Runtime.Remote.RepairComplete(dock.shipId, dock.id);
					this.Game.UserShip.UpdateUserShip(new List<UserShip>
					{
						repair2.shipVO
					});
					this.Game.RepairDock = new ObservableCollection<RepairDockInfo>(repair2.repairDockVo.Where((RepairDockInfo i) => i.locked == 0));
					this.Game.User.UpdateUserInfo(repair2.userVo);
					MessagingCenter.Send<LogMessage>(new LogMessage("[修理]完成" + dock.Name + "修理"), "Log");
				}
				dock = null;
			}
			IEnumerator<RepairDockInfo> enumerator = null;
			int num = this.Game.RepairDock.Count((RepairDockInfo i) => i.IsEmpty);
			int qn = (this.RepairQueue.Count < num) ? this.RepairQueue.Count : num;
			for (int j = 0; j < qn; j++)
			{
				UserShip ship = this.RepairQueue.Dequeue();
				await this.Game.Repair(ship);
			}
			IEnumerable<UserShip> enumerable = from i in this.Game.UserShip
			where !i.IsInRepair && i.fleetId == 0 && !this.Game.CampaignTask.Fleet.Contains(i.id) && 0 < i.RepairTime && i.RepairTime < this.MinHours * 3600 + this.MinMinutes * 60 && i.isLocked
			select i;
			IEnumerable<UserShip> enumerable2;
			if (!this.RepairOrder)
			{
				enumerable2 = from i in enumerable
				orderby i.RepairTime descending
				select i;
			}
			else
			{
				enumerable2 = from i in enumerable
				orderby i.RepairTime
				select i;
			}
			enumerable = enumerable2;
			foreach (UserShip ship2 in enumerable)
			{
				await this.Game.Repair(ship2);
			}
			IEnumerator<UserShip> enumerator2 = null;
		}

		// Token: 0x060005AE RID: 1454 RVA: 0x000577D5 File Offset: 0x000559D5
		public RepairTask()
		{
			this.<Id>k__BackingField = 5;
			this.<Type>k__BackingField = "RepairTask";
			this.<RepairQueue>k__BackingField = new Queue<UserShip>();
			base..ctor();
		}
	}
}
